package com.ihr360.oneclick.service.impl;

import com.ihr360.commons.lang.StringUtils;
import com.ihr360.oneclick.config.SmsConfig;
import com.ihr360.oneclick.dao.CompanyFreeMessageDao;
import com.ihr360.oneclick.dto.CompanyFreeMessageDto;
import com.ihr360.oneclick.exception.OneClickException;
import com.ihr360.oneclick.service.CompanyFreeMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CompanyFreeMessageServiceImpl implements CompanyFreeMessageService {

    @Autowired
    private CompanyFreeMessageDao companyFreeMessageDao;
    @Autowired
    private SmsConfig smsConfig;

    @Override
    @Transactional
    public Integer getCompanyFreeMessageCount(String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            throw new OneClickException("one_click_spreadsheet_data_error", "数据异常！");
        }
        CompanyFreeMessageDto companyFreeMessageDto = companyFreeMessageDao.findByCompanyId(companyId);
        if (companyFreeMessageDto == null) {
            companyFreeMessageDto = new CompanyFreeMessageDto();
            companyFreeMessageDto.setSmsFreeCount(smsConfig.getFreeSmsCount());
            companyFreeMessageDto.setCompanyId(companyId);
            companyFreeMessageDao.save(companyFreeMessageDto);
        }
        return companyFreeMessageDto.getSmsFreeCount();
    }
}
